//
//  CXDestinationDetailGoodTableViewCell.m
//  Travel
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXDestinationDetailGoodTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CXDestinationGoodModel.h"

@interface CXDestinationDetailGoodTableViewCell ()

@property (nonatomic, retain)UIImageView *img;
@property (nonatomic, retain)UILabel *labelTitle;
@property (nonatomic, retain)UILabel *blueLineLabel;
@property (nonatomic, retain)UILabel *dateLabel;
@property (nonatomic, retain)UILabel *dayCountLabel;
@property (nonatomic, retain)UILabel *pointOneLabel;
@property (nonatomic, retain)UILabel *pointTwoLabel;
@property (nonatomic, retain)UILabel *labelFoot;
@property (nonatomic, retain)UILabel *labelLike;
@property (nonatomic, retain)UIImageView *jianbianImage; //渐变image
@property (nonatomic, retain) UIView *layerView;


@end

@implementation CXDestinationDetailGoodTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.img = [[UIImageView alloc]init];
    [self.contentView addSubview:self.img];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    self.img.clipsToBounds = YES;
    
    
    self.jianbianImage = [[UIImageView alloc]init];
    [self.img addSubview:self.jianbianImage];
    self.jianbianImage.image = [UIImage imageNamed:@"jiabian2"];
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.numberOfLines = 2;
    [self.img addSubview:self.labelTitle];
    self.labelTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    
    
    self.blueLineLabel = [[UILabel alloc]init];
    self.blueLineLabel.backgroundColor = [UIColor colorWithRed:0.26 green:0.55 blue:0.58 alpha:1];
    [self.img addSubview:self.blueLineLabel];

    
    
    
    
    self.dateLabel = [[UILabel alloc]init];
    [self.img addSubview:self.dateLabel];

    
    self.dayCountLabel = [[UILabel alloc]init];
    [self.img addSubview:self.dayCountLabel];

    
    //初始化渐变属性
    self.layerView = [[UIView alloc] init];
    self.layerView.alpha = 0.5;
    self.layerView.backgroundColor = [UIColor blackColor];
    [self.img addSubview:self.layerView];
    
    self.pointOneLabel = [[UILabel alloc]init];
    self.pointOneLabel.backgroundColor = [UIColor colorWithRed:0.26 green:0.55 blue:0.58 alpha:1];
    [self.layerView addSubview:self.pointOneLabel];

    
    self.labelFoot = [[UILabel alloc]init];
    [self.layerView addSubview:self.labelFoot];

    
    
    self.pointTwoLabel = [[UILabel alloc]init];
    self.pointTwoLabel.backgroundColor = [UIColor colorWithRed:0.26 green:0.55 blue:0.58 alpha:1];
    [self.layerView addSubview:self.pointTwoLabel];

    
    self.labelLike = [[UILabel alloc]init];
    [self.layerView addSubview:self.labelLike];

    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    //夜间模式
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
    }else
    {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    }
    
    self.img.frame = CGRectMake(20, 15, WIDTH - 40, WIDTH / 3 + 20);
    
    self.jianbianImage.frame = CGRectMake(0, 0, WIDTH - 40, self.img.frame.size.height);
    
    CGFloat w = self.img.frame.size.width;
    
    CGFloat hTitle = [self heightWithText:self.model_good.name fontSize:18 Width:w - 40];
    
    self.labelTitle.frame = CGRectMake(20, 15, w - 40 , hTitle);
    self.labelTitle.textColor = [UIColor colorWithRed:0.88 green:0.9 blue:0.91 alpha:1];
    
    self.blueLineLabel.frame = CGRectMake(self.labelTitle.frame.origin.x - 5, self.labelTitle.frame.origin.y + self.labelTitle.frame.size.height + 10, 5, 10);
    
    self.dateLabel.frame = CGRectMake(self.blueLineLabel.frame.origin.x + self.blueLineLabel.frame.size.width + 5, self.blueLineLabel.frame.origin.y, 70, self.blueLineLabel.frame.size.height);
    self.dateLabel.font = [UIFont systemFontOfSize:10];
    self.dateLabel.textColor = [UIColor colorWithRed:0.88 green:0.9 blue:0.91 alpha:1];
    
    
    self.dayCountLabel.frame = CGRectMake(self.dateLabel.frame.origin.x + self.dateLabel.frame.size.width + 10, self.dateLabel.frame.origin.y, 60, self.dateLabel.frame.size.height);
    self.dayCountLabel.textColor = [UIColor colorWithRed:0.88 green:0.9 blue:0.91 alpha:1];
    self.dayCountLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
    
    
    //设置frame
    self.layerView.frame = CGRectMake(0, self.img.frame.size.height - 30 , w, 20);
    
    
    self.pointOneLabel.frame = CGRectMake(self.blueLineLabel.frame.origin.x, 8, 5, 5);
    self.pointOneLabel.layer.masksToBounds = YES;
    self.pointOneLabel.layer.cornerRadius = self.pointOneLabel.frame.size.width / 2;
    
    self.labelFoot.frame = CGRectMake(self.pointOneLabel.frame.origin.x + self.pointOneLabel.frame.size.width + 5, self.pointOneLabel.frame.origin.y, 80, self.pointOneLabel.frame.size.height);
    self.labelFoot.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
    self.labelFoot.textColor = [UIColor colorWithRed:0.88 green:0.9 blue:0.91 alpha:1];
    
    self.pointTwoLabel.frame = CGRectMake(self.labelFoot.frame.origin.x + self.labelFoot.frame.size.width + 5, self.pointOneLabel.frame.origin.y, 5 , 5);
    self.pointTwoLabel.layer.masksToBounds = YES;
    self.pointTwoLabel.layer.cornerRadius = self.pointTwoLabel.frame.size.width / 2;
    
    
    self.labelLike.frame = CGRectMake(self.pointTwoLabel.frame.origin.x + self.pointTwoLabel.frame.size.width + 5, self.pointTwoLabel.frame.origin.y, 100, self.labelFoot.frame.size.height);
    self.labelLike.font =  [UIFont fontWithName:@"Helvetica-Bold" size:10];
    
    self.labelLike.textColor = [UIColor colorWithRed:0.88 green:0.9 blue:0.91 alpha:1];
    
}

- (void)setModel_good:(CXDestinationGoodModel *)model_good
{
    _model_good = model_good;
    [self.img sd_setImageWithURL:[NSURL URLWithString:self.model_good.cover_image_default] placeholderImage:[UIImage imageNamed:@"load"]];
    self.labelTitle.text = self.model_good.name;
    self.dayCountLabel.text = [NSString stringWithFormat:@"%@%@", self.model_good.day_count, @"天"];
    self.labelLike.text = [NSString stringWithFormat:@"%@ %@", self.model_good.recommendations, @"喜欢"];
    self.labelFoot.text = [NSString stringWithFormat:@"%@ %@", self.model_good.waypoints, @"足迹"];
}

//计算文本高度的方法
- (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
